$(function(){
	$("#loading-first").fadeOut(500);
	var qd=[118.57, 24.82];
	var map = new AMap.Map('map', {
		resizeEnable: true,
		zoom: 10,
		center: qd
	});
	var marker;
	
    {if $Think.get.oil_station_id}
		var where_json=JSON.stringify({oil_station_id:["in","{$Think.get.oil_station_id}"]})
		Load()
		$.ajax({
			type:'post',
			data:{
				page:1,
				pagnum:10,
				where_json:where_json
			},
			url:"{:url('adminapi/Oilstation/oilStationList')}",
			success:function(res){
				console.log(res)
				Load(1)
				if(res.code==1){
					var detail;
					for(var ii=0;ii<res.list.length;ii++){
						if(res.list[ii].oil_station_id=={$Think.get.oil_station_id}){
							detail=res.list[ii]
						}
					}
					marker = new AMap.Marker({ //添加自定义点标记
				        map: map,
				        position: [detail['lon'], detail['lat']], //基点位置
				        offset: new AMap.Pixel(-17, -42)//相对于基点的偏移位置
				        
				    });
					for(var key in detail){
						$(`input[name=${key}]`).val(detail[key])
						$(`select[name=${key}]`).val(detail[key])
						$(`textarea[name=${key}]`).val(detail[key])
					}
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	
	{/if}
	
	var clickEventListener = map.on('click', function(e) {
	 	$('input[name="lon"]').val(e.lnglat.getLng())
	 	$('input[name="lat"]').val(e.lnglat.getLat())
	 	if(marker){
	 	map.remove(marker)
	 	}
	 	marker = new AMap.Marker({ //添加自定义点标记
	        map: map,
	        position: [e.lnglat.getLng(), e.lnglat.getLat()], //基点位置
	        offset: new AMap.Pixel(-17, -42)//相对于基点的偏移位置
	        
	    });
       console.log( e.lnglat.getLng() + ',' + e.lnglat.getLat())
    });
	var data={}
	{if $Think.get.oil_station_id}
	$('.keep2').on('click',function(){
		data.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				data[$(this).attr('name')]=$(this).val()
			}
		})
		data.scale=$('select[name="scale"]').val()
		data.remark=$('textarea').val()
		console.log(data)
		repairexit(data)
		
	})
	function repairexit(data){
		Load()
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Oilstation/edit')}",
			data:data,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
					Msg('保存成功')
					window.history.go(0)
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	
	
	{else/}
	$('.keep').on('click',function(){
		data.company_id=$('.company_all_id').val();
		$('input').each(function(){
			if($(this).attr('name')){
				data[$(this).attr('name')]=$(this).val()
			}
		})
		data.scale=$('select[name="scale"]').val()
		data.remark=$('textarea').val()
		console.log(data)
		
		repairadd(data)
		
	})
	
	function repairadd(data){
		Load()
		$.ajax({
			type:'post',
			url:"{:url('adminapi/Oilstation/add')}",
			data:data,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
					Msg('添加成功')
					window.location.href='{:url("index")}'
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	{/if}
})

